#include "ThreadPool.h"

int removeFunc(int id_now,const char *arg,const char* user,MYSQL *mysql)
{
    //设置返回值，在多参数情况下只保存最后一次失败的值
    int retval = 0;
    char argcpy[1024];
    strcpy(argcpy,arg);
    int idx = 0;
    char *p[1024];
    p[idx++] = strtok(argcpy," ");
    //printf("%s\n",p[0]);
    while((p[idx++] = strtok(NULL," ")) != NULL);
    //printf("%s\n",p[idx-1]);
    for(int i = 0; i < idx && p[i] != NULL; i++)
    {
        char name[1024] = {0};
        strcpy(name,p[i]);
        int id = getAbsolutePath(id_now,name,user,mysql);
        //int id = id_now;
        //如果id对应的文件不存在，则删除失败
        if(id == -1)
        {
            retval = -1;
            break;
        }
        //如果id对应的是目录，则删除失败；
        if(isDir(id,mysql))
        {
            retval = -1;
            break;
        }
        //如果id对应的tomb==1，则删除失败
        if(isTomb(id,mysql))
        {
            retval = -1;
            break;
        }
        //将int型转换成字符串型

        char sql[4096];
        bzero(sql,sizeof(sql));
        sprintf(sql,"update virtual_file set tomb = 1 where id =%d;",id);

        int qret = mysql_query(mysql,sql);
        MYSQL_QUERY_CHECK(qret,mysql);


    }
    return retval;
}
